const Product = require('../../db/productSchame');
module.exports = async(req, res) => {
    console.log(req.query);
    // 接受传递过来的页码
    let page = Number(req.query.page) || 1;
    // 每页显示多少条数据
    let size = req.query.size || 3;

    let min = req.query.price1 || 0;
    let max = req.query.price2 || 1000000000;
    // 总共多少条数据
    let total = await Product.count({
        title: new RegExp(req.query.title, 'gi'),
        price: { $gte: min, $lte: max }
    });
    console.log(total);
    // 共多少页
    let totalPage = Math.ceil(total / size);

    let skipdata = (page - 1) * size;

    // 查询当前的数据
    let relData = await Product.find({
        title: new RegExp(req.query.title, 'gi'),
        price: { $gte: min, $lte: max }
    }).limit(size).skip(skipdata);

    // 传递到页面上的对象
    let searchObj = {
        total: total,
        page: page,
        totalPage: totalPage,
        size: size,
        productlist: relData,
        price1: min,
        price2: max,
        title: req.query.title

    };
    // 渲染页面数据
    res.render('./product/searchproduct.ejs', searchObj);

}